package com.rfid.analysis;

import android.util.Log;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * @author Administrator
 * @date 2020/9/17
 */
public class HandlerEnd extends Handler {
    @Override
    public void handleRequest(Message request) {
        request.setCurrentNodeHandler(this);
        ByteBuffer byteBuffer = request.getBuffer();
        byte[] end = getEnd();
        if (byteBuffer.position() + end.length <= byteBuffer.limit()) {
            byte[] read = new byte[end.length];
            byteBuffer.get(read);
            if (Arrays.equals(end, read)) {
                //读取成功
                request.addData(read);
                request.setHasComplete(true);
                if (getNext() != null) {
                    getNext().handleRequest(request);
                }
            } else {
                //没有找到表示数据错误应该丢掉
                request.getFirstHandler().handleRequest(request);
            }
        } else {
            byteBuffer.compact();
        }
    }

    private byte[] getEnd() {
        return new byte[]{0x7E};
    }
}